﻿using Autofac;
using Common;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Models.PrintRecord;

namespace ZoneLine.Printers
{
    public class SQQCPrinter : IPrinter
    {
        public string PrintLabel(string path, string printerName, int index)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var (paras, code) = GetPara(index);
            PrinterHelper.PrinterLabel(path, printerName, paras);
            return code;
        }

        public void PrintLabelShow(string path, string printerName, int index)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var (paras, code) = GetPara(index);
            PrinterHelper.PrinterLabelShow(path, printerName, paras);
        }

        private (Dictionary<string, string>, string) GetPara(int index)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var configD = config.Sqqc_Config;
            var paras = new Dictionary<string, string>();
            paras.Add("LBJHM_N2", configD.PartNumber);
            paras.Add("FXH_N2", configD.ValveNumber);
            // 可追溯条码 生成
            var serial = GetSerial(index);
            paras.Add("KZSHM_N2", serial);
            paras.Add("SCRQ_N2", DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00"));
            paras.Add("GYSJC_N2", configD.SupplierAbbreviation);
            var partNumTemp = configD.PartNumber.Substring(configD.PartNumber.Length - 4);
            var code = $"{configD.PartIdentificationCode}{partNumTemp}{configD.SupplierCode}{serial}";
            paras.Add("EWM_N2", code);
            var para = StationEnableConfig.GetPara();
            if (Cache.WorkDirection == WorkDirectionEnum.Left)
            {
                paras.Add("JQFX_N2", "L");
            }
            else
            {
                paras.Add("JQFX_N2", "R");
            }
            SQRecord sQRecord = new SQRecord();
            sQRecord.Code = code;
            sQRecord.SerialNumber = index;
            sQRecord.PartIdentificationCode = configD.PartIdentificationCode;
            sQRecord.PartNumber = configD.PartNumber;
            sQRecord.SupplierAbbreviation = configD.SupplierAbbreviation;
            sQRecord.SupplierCode = configD.SupplierCode;
            sQRecord.TemplatePath = configD.TemplatePath;
            sQRecord.TemplatePathR = configD.TemplatePathR;
            sQRecord.TraceabilityNumber = configD.TraceabilityNumber;
            sQRecord.ValveNumber = configD.ValveNumber;
            DBContext.DbConfig.DbContext.Insert<SQRecord>(sQRecord).ExecuteInserted();
            return (paras, code);
        }

        private string GetSerial(int index)
        {
            var s = (byte)((DateTime.Now.Year - 2010) + 64);
            var year = Encoding.ASCII.GetString(new byte[] { s });
            var mouth = DateTime.Now.Month.ToString("X");
            var day = DateTime.Now.Date.ToString("dd");

            //var station=ContainerServer.Container.Resolve<>

            return $"{year}{mouth}{day}{index.ToString("0000")}";
        }
    }
}